﻿using IsmlTools.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IsmlTools.Bil
{
    public class PictureManager
    {
        public void MapHelper(String sourceFileName, string destinationFileName, String languageCode, string[] names, Font font)
        {
            using (Image mapimage = Image.FromFile(sourceFileName))
            {
                using (Bitmap mapbitmap = new Bitmap(mapimage, mapimage.Width, mapimage.Height))
                {
                    using (Graphics mapgraphics = Graphics.FromImage(mapbitmap))
                    {

                        font = new Font("宋体", 10, FontStyle.Regular);

                        if (names.Length <= 3)
                            mapgraphics.FillRectangle(Brushes.White, 378, 362, 235, 69);
                        else if (names.Length == 4)
                            mapgraphics.FillRectangle(Brushes.White, 378, 362, 333, 69);
                        else if (names.Length >= 6)
                        {
                            mapgraphics.FillRectangle(Brushes.White, 254, 362, 119, 47);
                            mapgraphics.FillRectangle(Brushes.White, 392, 362, 119, 51);
                            mapgraphics.FillRectangle(Brushes.White, 528, 362, 119, 43);
                            mapgraphics.FillRectangle(Brushes.White, 528, 362, 179, 22);
                            if (names.Length == 6)
                                mapgraphics.FillRectangle(Brushes.White, 643, 384, 175, 22);
                            if (names.Length == 7)
                                mapgraphics.FillRectangle(Brushes.White, 667, 387, 155, 19);
                        }

                        // 可以使用MeasureString来计算宽度,第六个可能要单独计算
                        for (int i = 0; i < names.Length; i++)
                        {
                            if (i == 6)
                                //graphics.DrawString(names[i], font, Brushes.Black, (256 + (7 / 2) * 138), (366 + (7 % 2) * 22));
                                mapgraphics.DrawString(names[i], font, Brushes.Black, new Rectangle(256 + (7 / 2) * 138, (366 + (7 % 2) * 22), 119, 19));
                            else if (names.Length == 2)
                                //graphics.DrawString(names[i], font, Brushes.Black, 378, 365 + i * 30);
                                mapgraphics.DrawString(names[i], font, Brushes.Black, new Rectangle(378, 365 + i * 30, 119, 19));
                            else if (names.Length == 3)
                                //graphics.DrawString(names[i], font, Brushes.Black, 378, 360 + i * 25);
                                mapgraphics.DrawString(names[i], font, Brushes.Black, new Rectangle(378, 360 + i * 25, 119, 19));
                            else if (names.Length > 5)
                                //graphics.DrawString(names[i], font, Brushes.Black, (256 + (i / 2) * 138), (366 + (i % 2) * 22));
                                mapgraphics.DrawString(names[i], font, Brushes.Black, new Rectangle((256 + (i / 2) * 138), (366 + (i % 2) * 22), 119, 19));
                            else if (names.Length == 4)
                            {
                                //graphics.DrawString(names[i] + "，" + names[i + 1], font, Brushes.Black, 378, 365 + (i / 2) * 30);
                                mapgraphics.DrawString(names[i] + "，" + names[i + 1], font, Brushes.Black, new Rectangle(378, 365 + (i / 2) * 30, 119, 19));
                                i++;
                            }
                        }
                        mapbitmap.Save(destinationFileName);
                    }
                }
            }
        }

        public void ChartHelper(String sourceFileName, string destinationFileName, String languageCode, string[] names, Font font)
        {
            using (Image chartimage = Image.FromFile(sourceFileName))
            {
                using (Bitmap chartbitmap = new Bitmap(chartimage, chartimage.Width, chartimage.Height))
                {
                    using (Graphics chartgraphics = Graphics.FromImage(chartbitmap))
                    {
                        font = new Font("宋体", 8, FontStyle.Regular);

                        if (names.Length <= 4)
                            chartgraphics.FillRectangle(Brushes.White, 2, chartbitmap.Height - 31, chartbitmap.Width - 4, 30);
                        else if (names.Length <= 6)
                            chartgraphics.FillRectangle(Brushes.White, 1, chartbitmap.Height - 51, chartbitmap.Width - 4, 50);
                        else if (names.Length > 6)
                            chartgraphics.FillRectangle(Brushes.White, 1, chartbitmap.Height - 71, chartbitmap.Width - 4, 70);
                        // +
                        string legendpath = Application.StartupPath + @"\Image\Chart\Legend\";
                      
                        for (int i = 0; i < names.Length; i++)
                        {
                            if (names.Length <= 3)
                            {
                                var legendFileName = legendpath + "c3" + (i + 1) + ".jpg";
                                using (Bitmap legendbitmap = new Bitmap(legendFileName))
                                {
                                    var destRect = new Rectangle((chartbitmap.Width / names.Length) * i + 30, chartbitmap.Height - 27, legendbitmap.Width, legendbitmap.Height);
                                        chartgraphics.DrawImage(legendbitmap, destRect, 0, 0, legendbitmap.Width, legendbitmap.Height, GraphicsUnit.Pixel);
                                    chartgraphics.DrawString(names[i], font, Brushes.Black, (chartbitmap.Width / names.Length) * i + 60, chartbitmap.Height - 26);
                                }
                            
                            }
                            else switch (names.Length)
                            {
                                case 4:
                                    {

                                        string legendFileName = legendpath + "c3" + (i / 2 + 1) + ".jpg";
                                        using (Bitmap legendbitmap = new Bitmap(legendFileName)) {
                                            var destRect = new Rectangle((chartbitmap.Width / 2) * (i / 2) + 30, chartbitmap.Height - 27, legendbitmap.Width, legendbitmap.Height);
                                            chartgraphics.DrawImage(legendbitmap, destRect, 0, 0, legendbitmap.Width, legendbitmap.Height, GraphicsUnit.Pixel);
                                            chartgraphics.DrawString(names[i] + "，" + names[i + 1], font, Brushes.Black, (chartbitmap.Width / names.Length) * (i / 2) + 60, chartbitmap.Height - 26);
                                            i++;
                                        }
                                    }
                                    break;
                                case 6:
                                    {
                                        string legendFileName = legendpath + "c6" + (i + 1) + ".jpg";
                                        using (var legendbitmap = new Bitmap(legendFileName))
                                        {
                                            var destRect = new Rectangle((chartbitmap.Width / 3) * (i % 3) + 30, chartbitmap.Height - 48 + 20 * (i / 3), legendbitmap.Width, legendbitmap.Height);
                                            chartgraphics.DrawImage(legendbitmap, destRect, 0, 0, legendbitmap.Width, legendbitmap.Height, GraphicsUnit.Pixel);
                                            chartgraphics.DrawString(names[i], font, Brushes.Black, (chartbitmap.Width / 3) * (i % 3) + 60, chartbitmap.Height - 47 + 20 * (i / 3));
                                        }
                                    }
                                    break;
                                case 7:
                                    {
                                        string legendFileName = legendpath + "c7" + (i + 1) + ".jpg";
                                        using (Bitmap legendbitmap = new Bitmap(legendFileName))
                                        {
                                            Rectangle destRect = new Rectangle((chartbitmap.Width / 3) * (i % 3) + 30, chartbitmap.Height - 68 + 20 * (i / 3), legendbitmap.Width, legendbitmap.Height);
                                            chartgraphics.DrawImage(legendbitmap, destRect, 0, 0, legendbitmap.Width, legendbitmap.Height, GraphicsUnit.Pixel);
                                            chartgraphics.DrawString(names[i], font, Brushes.Black, (chartbitmap.Width / 3) * (i % 3) + 60, chartbitmap.Height - 67 + 20 * (i / 3));
                                        }
                                    }
                                    break;
                            }

                        }

                        chartbitmap.Save(destinationFileName);
                    }
                }
            }
        }
    }
}
